package com.apexsoft.monitor.front.console.log.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.BaseFrontController;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.JsonResult;
import com.apexsoft.monitor.common.MonitorConstant;
import com.apexsoft.monitor.front.console.log.om.Log;
import com.apexsoft.monitor.front.console.log.service.LogService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author lsj
 *
 */
@Controller("logController")
@RequestMapping("/console/log")
public class LogController extends BaseFrontController {
	protected final transient Logger log = LogManager.getRootLogger();
	@Autowired
	@Qualifier("logService")
	private LogService logService = null;
	
	@RequestMapping(value = "/list")
	public String list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "console/logConsole/list";
	}

	/**
	 * 跳转->实时日志页面
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
     */
	@RequestMapping(value = "/realtime")
	public String realtime(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return "console/logConsole/realtime";
	}

    /**
     * 获取日志的最大ID号
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/getLogLastId")
    public void getLastId(HttpServletRequest request,HttpServletResponse response){
        JSONObject result = new JSONObject();
        String app_code = request.getParameter("app_code");
        String node_code = request.getParameter("node_code");
        String level = request.getParameter("level");
        String id = logService.queryLogByLastId(app_code,node_code,level);
        if(id==null){
            id = "-1";
        }
        result.put("id",id);
        this.printJSON(response, result);
    }

    /**
     * 获取实时日志
     * @param request
     * @param response
     */
	@ResponseBody
	@RequestMapping(value = "/getRealTimeLog")
	public void getRealTimeLog(HttpServletRequest request,HttpServletResponse response){
        String app_code = request.getParameter("app_code");
        String node_code = request.getParameter("node_code");
        String level = request.getParameter("level");
        String task_code = request.getParameter("task_code");
        String id = request.getParameter("id");
		JSONObject result= logService.queryLogAfterId(app_code,node_code,level,id,task_code);
		this.printJSON(response, result);
	}

	@ResponseBody
	@RequestMapping(value = "/queryTask")
	public void queryTask(HttpServletRequest request,HttpServletResponse response){
		String node_code = request.getParameter("node_code");
		JSONObject result= logService.queryLogTask(node_code);
        JSONArray array = new JSONArray();
        JSONObject nullObj = new JSONObject();
        nullObj.put("value","");
        nullObj.put("task_name","----请选择----");
        array.add(nullObj);
        array.addAll(1,result.getJSONArray("root"));
		this.printJSON(response, array);
	}

	
	@ResponseBody
	@RequestMapping(value="/logData")
	public void logData(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception
	{
		String key = request.getParameter("key");
		if(key!=null)key=key.trim();
		JSONObject result = logService.queryLogByPage(key);
		this.printJSON(response, result);
	}
	
	@ResponseBody
	@RequestMapping(value = "/detailView/{id}")
	public void detailView(@PathVariable String id,HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		
		JSONObject result = new JSONObject();
		Log tLog = logService.queryDetail(Integer.parseInt(id));
		if(tLog!=null){
			result.put("success", true);
			result.put("data", tLog);
		}else{
			result.put("success", false);
		}
		System.out.println(result.toJSONString());
		this.printJSON(response, result);
	}

	@ResponseBody
	@RequestMapping(value = "/listByPageForEasyui")
	public void listByPageForEasyui (HttpServletRequest request, HttpServletResponse response) throws Exception {
		String pageStr = request.getParameter("pageNo");
		String pageCountStr = request.getParameter(Result.PAGE_SIZE);
		String appCode = request.getParameter("appCode");
		String nodeCode = request.getParameter("nodeCode");
		String level = request.getParameter("level");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String content = request.getParameter("content");
		int page = 0, pageCount = 0;
		JSONObject result = new JSONObject();
		try {
			page = Integer.valueOf(pageStr);
			pageCount = Integer.valueOf(pageCountStr);
			result = logService.listByPageForEasyui(page, pageCount,appCode, nodeCode ,level, startTime, endTime,content).getJSONObject("obj");
		} catch (Exception e) {
			log.error(e.getMessage());
			result = new JsonResult().getExceptionResult(e);
		}
		this.printJSON(response, result);
	}

	@ResponseBody
	@RequestMapping(value="/queryLogs")
	public void queryLogs(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception
	{
		String id = request.getParameter("id");
		String page = request.getParameter("page");
		String type = request.getParameter("type");
		if(id!=null)id=id.trim();
		JSONObject result = logService.queryLogForLimit(Integer.parseInt(id),Integer.parseInt(page), MonitorConstant.LOG_SIZE,type);
		this.printJSON(response, result);
	}
}
